<?php

namespace app\api\controller;

use app\common\controller\Api;
use Darabonba\GatewaySpi\Models\InterceptorContext\request;
use think\Db;

/**
 * 记录
 * Class
 * @package app\api\controller
 */
class Logs extends Api
{

	protected $noNeedLogin = ['access', 'phone'];
	protected $noNeedRight = '*';

	/**
	 * 手机号存储
	 */
	public function phone()
	{
		$phone = $this->request->post("phone", "");
		if (!$phone) {
			$this->error("手机号不能为空");
		}
		$data = ["phone" => $phone, "create_time" => time()];
		$id = Db::name("reserve_phone")->insertGetId($data);
		$this->success("预留成功", $id);
	}

	/**
	 * 访问
	 */
	public function access()
	{

		$encode = $this->request->post("equipment_number", '');
		$ip = $_SERVER['REMOTE_ADDR'];

		$nowTime = time();
		$find = Db::name("logs_access")->where("ip", $ip)->order("create_time DESC")->find();
		if ($find) {
			$checkTime = strtotime('+1 minutes', $find["create_time"]);
			if ($checkTime > $nowTime) {
				$this->error("重复记录");
			}
		}
		$data = [
			"ip" => $ip,
			"codes" => $encode,
			"times" => strtotime(date("Y-m-d", time())),
			"create_time" => time()
		];
		Db::name("logs_access")->insert($data);
		$this->success("访问记录", $ip);
	}
}
